package ControlLayer;//makes this class a part of the ControlLayer package
import ModelLayer.*;
import java.util.*;

/**
 * 
 * Controller class for the Loans
 * <p>
 * Accepts input user data from the <code>LoanUI</code> class(TUI Layer) and manages the data stored in <code>Loan</code> class(ModelLayer).
 * <br>
 * @author (dmaj0913 - group 2)
 * @version (2013.11.08)
 */
public class LoanCtr
{
  private ArrayList<Loan> loanList;
    
  /**
   * Constructor assigns a collection of loans to the loanList.  
   * @param backup stores a recently created new empty collection of DVDs or a backup of a collection managed previously. 
   */
  public LoanCtr(ArrayList<Loan> backup)
  {
    this.loanList = backup;
  }
  /**
   * Finds a specific loan in the collection and returns it.
   * @param id stores the id of the desired loan.
   * @returns an object of type Loan with the desire loan.
   */
  public Loan findLoan(long no)
  {
    Iterator<Loan> it = this.loanList.iterator();
    while (it.hasNext())
    {
      Loan test = (Loan)it.next();
      if (test.getId() == no) {
        return test;
      }
    }
    return null;
  }
  
  /**
   * Creates new loan and adds it to the collection.
   * @param id stores the id of the loan.
   * @param date stores a character string of the date of the loan.
   * @param period stores a character string of the period of the loan.
   * @param state stores a character string of the state of the loan.
   * @param person stores an object of type person of the friend who borrowed the DVD Copy.
   * @param copy stores an object of type Copy that stores the desired copy.
   */
  public void createLoan(long id, String date, String period, String state, Person person, Copy copy)
  {
    Loan newLoan = new Loan(id, date, period, state, copy, person);
    this.loanList.add(newLoan);
  }
  
  /**
   * Finds a loan by its ID and updates it.
   * @param id stores the id of an existing loan.
   * @param newid stores the new desired id.
   * @param date stores a character string of the new date of the loan.
   * @param period stores a character string of the new period of the loan.
   * @param state stores a character string of the new state of the loan.
   */
  public void updateLoan(long id, long newid, String date, String period, String state)
  {
    Iterator<Loan> it = this.loanList.iterator();
    while (it.hasNext())
    {
      Loan toupdate = (Loan)it.next();
      if (toupdate.getId() == id)
      {
        toupdate.setId(newid);
        toupdate.setBorrowDate(date);
        toupdate.setPeriod(period);
        toupdate.setState(state);
      }
    }
  }
  /**
   *Returns the current collection of all loans.
   *@return ArrayList of type Loan containing all current loans. 
   */
  public ArrayList<Loan> listAllLoans()
  {
    return this.loanList;
  }
  /**
   * Deletes a loan by its unique id.
   * @param id takes the id of the loan that has to be deleted.
   */
  public void deleteLoan(long id)
  {
    Iterator<Loan> it = this.loanList.iterator();
    while (it.hasNext())
    {
      Loan todelete = (Loan)it.next();
      if (todelete.getId() == id) {
        it.remove();
      }
    }
  }
}